package mavenProject.DAO;

import java.util.ArrayList;

import mavenProject.Model.Question;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class QuestionDAOImpl extends HibernateDaoSupport implements QuestionDAO {

	private HibernateTemplate hibernateTemplate;
	
	
	//@Autowired
	//public void setSessionFactory(SessionFactory sessionFactory) {
	//	hibernateTemplate = new HibernateTemplate(sessionFactory);
	//}
	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}

	
	
	public Question getQuestion(int id) {
		//return hibernateTemplate.get(Question.class, id);  // working query
		
		//return (Question) hibernateTemplate.findByNamedParam("from Question Where ID = :ID", "ID", ID).get(0);
		Criteria criteria = getSession().createCriteria(Question.class);
		
		//Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Question.class);
		criteria.add(Restrictions.eq("id", id));
		return (Question) criteria.uniqueResult();
		
		//Criteria criteria = hibernateDaoSupport.getSessionFactory().getCurrentSession().createCriteria(Question.class);
		//criteria.add(Restrictions.eq("questionID", id));
		//return (Question) criteria.uniqueResult();
				
	}

	public void addQuestion(Question question) {
		hibernateTemplate.save(question);
	}

	
	@SuppressWarnings("unchecked")
	public ArrayList<Question> getAllQuestions() {
		Criteria criteria = getSession().createCriteria(Question.class);
		return  (ArrayList<Question>) criteria.list();
		
		//return (ArrayList<Question>) hibernateTemplate.find("from Question");
		
		//Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Question.class);
		//criteria.add(Restrictions.isNotNull(""));
		///return criteria.list();
		
	}


}
